package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/report")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private OrderService orderService;

    // 统计会员 折线图
    @GetMapping("/getMemberReport")
    public Result getMemberReport() {

        Calendar cal = Calendar.getInstance();
        List<String> months = new ArrayList<>();

        cal.add(Calendar.MONTH, -12);
        for (int i = 0; i < 12; i++) {

            cal.add(Calendar.MONTH, 1);

            months.add(new SimpleDateFormat("yyyy-MM").format(cal.getTime()));
        }
        HashMap<String, Object> member = new HashMap<>();
        member.put("months", months);
        List<Integer> countMember = memberService.countMember(months);

        member.put("memberCount", countMember);

        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, member);

    }

    // 套餐 饼 形 图
    @GetMapping("/getSetmealReport")
    public Result getSetmealReport() {

        List<Map<String, Object>> setmealCount = orderService.countSetmeal();
        List<String> setmealNames = new ArrayList<>();
        for (Map<String, Object> map : setmealCount) {
            setmealNames.add(String.valueOf(map.get("name")));
        }


        Map<String, Object> map = new HashMap<>();
        map.put("setmealNames", setmealNames);
        map.put("setmealCount", setmealCount);
        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
    }

}
